/***************************************************************************
 *   Copyright (C) 2007 by Gaetano Mendola  *
 *   mendola@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include <assert.h>
#include <iostream>

#include "flurry.h"

#include "../player.h"

#include "../events/eventdispatcher.h"

#include "../stats/multiplier.h"

#include "windfury.h"

namespace Buffs {

Flurry::Flurry(unsigned char aIncreaseAttackSpeed)
:Buff("Flurry"),
 theCharges(3),
 theMultiplier(1000.0 * 100.0/(100.0+aIncreaseAttackSpeed))
{
    Events::EventDispatcher::instance().subscribe(Events::Event::SWING, this);
}


Flurry::~Flurry()
{
    Events::EventDispatcher::instance().unsubscribe(Events::Event::SWING, this);
}

void Flurry::refresh()
{
    theCharges = 3;
}

void Flurry::gained()
{
    assert(thePlayer);

    //Looking for WEAPON_SPEED stat on player stat list in order
    //to add a modifier

    Player::stat_iterator it     = thePlayer->begin_stats();
    Player::stat_iterator it_end = thePlayer->end_stats();

    for (/*void*/; it != it_end; ++it) {
        if ( (*it)->type() == Stats::WEAPON_SPEED ) {
            //I have to add to this stat the multiplier
            (*it)->addModifier(&theMultiplier);

        }
    }
}

void Flurry::lost()
{
    assert(thePlayer);

    //Looking for WEAPON_SPEED stat on player stat list in order
    //to add a modifier

    Player::stat_iterator it     = thePlayer->begin_stats();
    Player::stat_iterator it_end = thePlayer->end_stats();

    for (/*void*/; it != it_end; ++it) {
        if ( (*it)->type() == Stats::WEAPON_SPEED ) {
            //I have to add to this stat the multiplier
            (*it)->removeModifier(&theMultiplier);
        }
    }
}

void Flurry::notify(const Events::Event* anEvent)
{
    assert(thePlayer);

    Events::Event::TYPE myType = anEvent->type();

    if ( myType == Events::Event::SWING ) {
        //Need to check if WindfuryBuff is active
        if (thePlayer->buffActive(Buffs::WindFury::ID)) {
            return;
        }

        if (theCharges > 0) {
            --theCharges;
            if ( theCharges == 0 ) {
                Events::EventDispatcher::instance().unsubscribe(Events::Event::SWING, this);

                thePlayer->removeBuff(this);
            }
        }
    }
}


}

